<?php
/**
 * Created by PhpStorm.
 * User: zgh
 * Date: 2021-08-05
 * Time: 10:03
 */

namespace app\admin\service;


use app\admin\item\QuickMenuLoader;
use app\admin\pipeline\ItemAdapter;
use app\admin\validate\QuickMenuValidate;
use app\common\model\QuickMenuModel;


class QuickMenuService
{
    public static function getList($params)
    {

        $dataList = (QuickMenuModel::where([["status", "<>", -1]])->order(["weight" => "desc", "id" => "asc"])->select()->toArray());
        $total = count($dataList);
        return ['count' => $total, "dataList" => $dataList];
    }


    public static function save($params)
    {
        $loader = new QuickMenuLoader();
        $item = $loader->addVal($params)->addVerify(QuickMenuValidate::class)->loadItem();
        $adapter = new ItemAdapter(QuickMenuModel::class);
        $res = $adapter->save($item);
        if ($res) {
            return $adapter->getId();
        } else {
            return false;
        }
    }


    public static function del($params)
    {
        $adapter = new ItemAdapter(QuickMenuModel::class);
        return $adapter->whereUpdate([["id", "in", $params['id']]], ["status" => -1]);
    }


    public static function getAdminFastMenu($admin_id = 0)
    {
        if (empty($admin_id)) {
            $admin_id = LoginService::getAdminId();
        }
        $node_arr = LoginService::getAdminNodeArr($admin_id);
        $res_arr = [];
        $quick_arr = QuickMenuModel::where(["status" => 1])->order(["weight" => "desc", "id" => "asc"])->select();
        if ($quick_arr && $node_arr) {
            foreach ($quick_arr as $quick_row) {
                if (in_array($quick_row['url'], $node_arr)) {
                    $res_arr[] = $quick_row;
                }
            }
        }
        return $res_arr;
    }
}